package com.zuiacsn.forum.controller;

import com.zuiacsn.forum.common.AppResult;
import com.zuiacsn.forum.common.ResultCode;
import com.zuiacsn.forum.exception.ApplicationException;
import com.zuiacsn.forum.model.Board;
import com.zuiacsn.forum.service.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "板块相关接口")
@RestController
@RequestMapping("/board")
@Slf4j
public class BoardController {

    @Autowired
    IBoardService boardService;
    @Value(value = "${bit-forum.index.board-num:9}")
    Integer indexBoardNum;

    /**
     * 查询首页板块列表
     * @return
     */
    @GetMapping("/topList")
    @ApiOperation("获取首页板块列表")
    public AppResult<List<Board>> topList(){
        log.info("首页板块个数为"+indexBoardNum);
        List<Board> boards = boardService.selectByNum(indexBoardNum);
        if(boards==null){
            boards =  new ArrayList<Board>();
        }
        return AppResult.success(boards);
    }

    @ApiOperation("获取板块信息")
    @GetMapping("/getById")
    public AppResult<Board> getById(@ApiParam("板块id") @Param("id") @NonNull Long id){
        Board board = boardService.selectById(id);

        // 对查询结果进行校验
        if(board == null || board.getDeleteState() ==1){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        // 返回结果
        return AppResult.success(board);
    }


}
